
/***************************************************************************
 *   Copyright (C) 1997 to 2004 by Jonathan Duddington                     *
 *   email: jonsd@users.sourceforge.net                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write see:                           *
 *               <http://www.gnu.org/licenses/>.                           *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "wimp.h"
#include "dbox.h"
#include "os.h"
#include "werr.h"
#include "menu.h"
#include "font.h"
#include "template.h"

#include "narc.h"
#include "hdrs.h"

extern FOLDREC list_fr[N_LISTS];
extern int timezone_offset;
extern int today_date;   /* date when Pluto started running */
extern menu    menu_list;
extern menu menu_selection;
extern char menu_score_value[8];


extern char *path_choices;
extern OPTIONS options;
extern MNEM_TAB2 header_lines[];
extern char *strip_mailing_list;


static FILE *f_in;
static int lineno;
static char linebuf[80];


static char *setup_cmds[] = {
	"header_lines",
	"strip_mailing_list",
	NULL
};



static int fgetline()
/*******************/
{
	int  c;
	char *p;
	int  len;

	for(;;)
	{
		lineno++;
		fgets(linebuf,sizeof(linebuf),f_in);

		if(feof(f_in))
			return(-1);

		if(linebuf[0] == '$')
			return(1);   /* next command */

		if(linebuf[0] == '#')
			continue;    /* comment */

		/* strip trailing spaces */
		len=strlen(linebuf)-1;
		while((len > 0) && isspace(linebuf[len]))
		{
			linebuf[len--]=0;
		}


		p = linebuf;
		while(((c = *p) != 0) && (isspace(c)))
		{
			p++;
		}
		if(c != 0)
			break;   /* not a blank line */

	}

	return(0);
}  /* end of fgetline */



void setup_err()
/**************/
{
	werr(0,"Error in choices.setups, line %d",lineno);
}   /* end of setup_err */



void setup_header_lines()
/***********************/
{
	int  ix=0;
	int  len;
	int  param1;
	char *p;
	char buf[80];

	while(fgetline() == 0)
	{
		if(sscanf(linebuf,"%s %d",buf,&param1)!=2)
		{
			setup_err();
			continue;
		}

		if(ix < (N_MARKED_HEADER_LINES-1))
		{
			len = strlen(buf)+2;
			p = malloc(len+1);
			if(p != NULL)
			{
				sprintf(p,"%s: ",buf);

				header_lines[ix].mnem = p;
				header_lines[ix].value = param1;
				header_lines[ix].len = len;
				ix++;
			}
		}
	}
	header_lines[ix].mnem = NULL;
}  /* end of setup_header_lines */



void setup_string(char **ptr)
/***************************/
{
	int  len;
	char *p;

	while(fgetline() == 0)
	{
		len = strlen(linebuf);
		p = malloc(len+1);
		if(p != NULL)
		{
			strcpy(p,linebuf);
			*ptr = p;
		}
	}
}   /* setup_string */



void setup_read()
/***************/
{
	int  i;
	int  cmd;
	char *p;
	char command[128];

	lineno = 0;

	sprintf(command,"%s.setups",path_choices);
	f_in = fopen_werr(command,"r",NULL);
	if(f_in == NULL)
		return;

	while((i = fgetline()) >= 0)
	{
		if(i == 1)
		{
			/* lookup command */
			if(sscanf(&linebuf[1],"%s",command) != 1)
				continue;

			cmd = 0;
			while((p = setup_cmds[cmd++]) != NULL)
			{
				if(strcmp_lc(p,command) == 0)
					break;
			}

			if(p != NULL)
			{
				switch(cmd)
				{
				case 1:
					setup_header_lines();
					break;

				case 2:   /* strip mailing list */
					setup_string(&strip_mailing_list);
					break;
				}
			}
			else
			{
				werr(0,"Setup command line not recognised: %s",command);
			}
		}
	}



	fclose(f_in);
}   /* end of setup_read */




/*****************************************************************/

extern menu menu_main;
extern menu menu_prefs;
extern menu menu_list;
extern wimp_menustr *wmenu_display;
extern char desktop_font_name[];
extern unsigned int list_backg_rgb;
extern unsigned int alist_backg_rgb;
extern DISPLAY display_tab[N_DISPLAY_TAB];
extern int scale_x, scale_y;   /* font scale factors */

extern int lists_line_height;
extern int lists_font_offset;
extern int status_sprite_offset;
extern char *status_sprites;
extern int fontn[N_FONTS];
extern char *fontname[N_FONTS];
extern int font_spacing[N_FONTS];
extern int font_offset[N_FONTS];
extern int standard_font;


static dbox dbox_display;
static menu menu_display = 0;    /* list of display/sort types */
static menu menu_display1 = 0;   /* listof display/sort types, plus option to add more */
static int display_number;
static int display_dbox_field;
static FOLDREC *current_list;



#define N_FIELDNAMES 18

static MNEM_TAB fieldnames[N_FIELDNAMES+2] = {
	"",         0,
	"Date",     1,
	"Category", 2,
	"Address",  3,
	"Subject",    4,
	"Source",   5,
	"Length",   6,
	"Icon2",    7,
	"Month",    8,
	"Cat-Parent", 9,
	"Domain",  10,
	"Reply",   11,
	"Count",   12,
	"Author",  13,
	"Attach",  14,
	"Icon",    15,
	"Unread",  16,
	"Score",   17,
	"Year",    18,
	NULL,       -1,
};

static MNEM_TAB sortnames[] = {
	"",         0,
	"Date",     1,
	"Category", 2,
	"Address",  3,
	"Subject",    4,
	"Source",   5,
	"Length",   6,
	"Status",   7,
	"Month",    8,
	"Cat-Parent", 9,
	"Domain",  10,
	"Score",   11,
	"Year",    13,
	NULL,       -1,
};

static char *menustr_sort_fields = "(none),Date,Category,Address,Subject,Source,Length,Status,Month,Cat-parent,Domain,Score,Year";

static char *menustr_display_fields
= "Date,Category,Address,Subject,Source,Length,Icon2,Month,Cat-parent,Domain,Reply,Count,Author,Attach,Icon,Unread,Score,Year";


static char *thread_type_names[] = {
	"(none)", "Subject", "Status", "First_date", "Last_date", "Max_score", ""
};

static char *menustr_thread_types =
	"(none),Subject,Status,First_date,Last_date,Max_score";


static menu menu_sort_fields;
static wimp_menustr *wmenu_sort_fields;
static menu menu_display_fields;
static wimp_menustr *wmenu_display_fields;
static menu menu_thread_types;
static wimp_menustr *wmenu_thread_types;




static void decompile_display_line(DISPLAY_LEVEL *dline, char *buf)
/**********************************************************/
{
	char *d;
	int  code;
	char *p;

	buf[0] = 0;
	d = &dline->display_items[0];
	while((code = d[0]) > 0)
	{
		if(code <= N_FIELDNAMES)
			p = fieldnames[code].mnem;
		else
			p = "???";
		sprintf(buf,"%s %s",buf,p);

		if(d[1] >= 0)
		{
			sprintf(buf,"%s %d",buf,d[1]);
		}
		if(d[2] >= 0)
		{
			sprintf(buf,"%s %d",buf,d[2]);
		}
		d+=3;
	}
}   /* end of decompile_display_line */





void display_writeout()
/*********************/
{
	FILE *f;
	DISPLAY *d;
	DISPLAY_LEVEL *d_level;
	int  display_num;
	int  level;
	char *reverse;
	char buf[400];

	sprintf(buf,"%s.displays",path_choices);
	f = fopen_werr(buf,"w",NULL);
	if(f == NULL)
		return;

	fprintf(f,"# This is a textual representation of the !Pluto.Choices.Display\n# file.  It is not currently read by Pluto\n\n");

	for(display_num=0; display_num < N_DISPLAY_TAB; display_num++)
	{
		d = &display_tab[display_num];
		if(d->name[0] == 0)
			continue;

		fprintf(f,"$Sorting %s\n",d->name);
		fprintf(f,"width=%d\n",d->window_width/5);
		fprintf(f,"warn=%d %d\n",d->warn,0);
		fprintf(f,"direction=%d\n",d->direction);
		for(level=0; level<N_SORT_LEVELS; level++)
		{
			d_level = &d->level[level];
			if(d_level->sort_field==0)
				continue;

			reverse="";
			if(d_level->sort_reverse)
				reverse=" reverse";

			fprintf(f,"sort=%s%s\n",sortnames[d_level->sort_field].mnem,reverse);
		}
		if(d->threading)
			fprintf(f,"threadsort=%s\n",thread_type_names[d->threading]);

		for(level=0; level<N_SORT_LEVELS; level++)
		{
			d_level = &d->level[level];
			if(d_level->sort_field==0)
				continue;

			decompile_display_line(d_level,buf);
			fprintf(f,"display=%s\n",buf);
		}
		fprintf(f,"\n");
	}
	fclose(f);
}   /* end of display_writeout */





void display_options_save()
/*************************/
{
	FILE *f;
	char fname[256];

	sprintf(fname,"%s.display",path_choices);
	f = fopen(fname,"w");
	if(f==NULL)
		return;

	fwrite(display_tab,1,sizeof(display_tab),f);
	fclose(f);

	display_writeout();
}   /* end of options_save */




void display_options_load()
/***********************/
{
	FILE *f;
	char fname[256];

	sprintf(fname,"%s.display",path_choices);
	f = fopen(fname,"r");

	if(f==NULL)
	{
		return;
	}

	memset(display_tab,0,sizeof(display_tab));
	fread(display_tab,1,sizeof(display_tab),f);
	fclose(f);
}   /* end of options_load */





static int compile_display_line(DISPLAY *dptr, int row, char *string)
/************************************************************/
{
	int  c;
	int  item;
	int  param;
	int  code;
	int  value;
	int  i;
	DISPLAY_LEVEL *dlevel;
	char name[80];

	dlevel = &dptr->level[row];
	item = -1;
	for(;;)
	{
		/* find start of next item */
		while(((c = *string) != 0) && (c <= ' ')) string++;
		if(c == 0)
			break;

		if(isdigit(c))
		{
			value = atoi(string);
			if(value > 255) value = 255;
			while(isdigit(*string)) string++;  /* skip to end of number */

			if(item < 0)
			{
				werr(0,"Row %d, Display Name missing before number",row+1);
				return(1);
			}

			param++;
			if(param > 2)
			{
				werr(0,"Row %d, Too many number parameters",row+1);
				return(1);
			}
			dlevel->display_items[item*3+param] = value;
		}
		else
		{
			i = 0;
			while((name[i] = *string) > ' ')
			{
				i++;
				string++;
			}
			name[i] = 0;

			code = lookup_mnem(fieldnames,name);
			if(code <= 0)
			{
				werr(0,"Row %d, Bad Display Name '%s'",row+1,name);
				return(1);
			}

			item++;
			if(item >= N_SORT_DISPLAY_ITEMS)
				return(0);

			param = 0;
			dlevel->display_items[item*3] = code;
			dlevel->display_items[item*3+1] = 0;
			dlevel->display_items[item*3+2] = 0;

		}
	}
	item++;
	dlevel->display_items[item*3] = 0;   /* terminator */
	return(0);
}   /* end of compile_display_line */




void make_display_menu()
/**********************/
{
	int  i;
	DISPLAY *dptr;

	if(menu_display != 0)
	{
		menu_submenu(menu_list,3,0);
		menu_dispose(&menu_display,0);
	}
	if(menu_display1 != 0)
	{
		menu_submenu(menu_main,3,0);
		menu_submenu(menu_prefs,4,0);
		menu_dispose(&menu_display1,0);
	}

	menu_display = menu_new("Sorting","");
	menu_display1 = menu_new("Sorting","Add Display");

	for(i=0; i<N_DISPLAY_TAB; i++)
	{
		dptr = &display_tab[i];

		if(dptr->name[0] != 0)
		{
			menu_extend(menu_display,dptr->name);
			menu_extend(menu_display1,dptr->name);
		}
	}

	menu_submenu(menu_list,3,menu_display);

	menu_submenu(menu_prefs,5,menu_display1);
	menu_submenu(menu_main,3,menu_prefs);

	wmenu_display = (wimp_menustr *)menu_syshandle(menu_display);
}   /* end of make_display_menu */



static void selected_thread_type(int *hits)
/*****************************************/
{
	DISPLAY *dptr;

	dptr = &display_tab[display_number];
	dptr->threading = hits[0];
	dbox_setfield(dbox_display,19,thread_type_names[hits[0]]);
}   /* end of selected_thread_type */



static void selected_display_field(int *hits)
/***********************************/
/* Called from unknown_event_handler.  Bodge to get menu on dialogue box */
{
	int  field;
	int  row;
	int  hit1;
	char string[16];
	DISPLAY *dptr;
	DISPLAY_LEVEL *dlevel;
	char buf[81];

	dptr = &display_tab[display_number];

	hit1 = hits[0]+1;   /* convert to range 1 .. */

	field = display_dbox_field - 4;
	row = field / 3;
	field = field % 3;

	dlevel = &dptr->level[row];
	if(field == 0)
	{
		dlevel->sort_field = hit1-1;
		dbox_setfield(dbox_display,display_dbox_field,sortnames[hit1-1].mnem);
	}
	else
	{
		sprintf(string,fieldnames[hit1].mnem);
		dbox_getfield(dbox_display,display_dbox_field,buf,sizeof(buf));
		sprintf(buf,"%s %s",buf,fieldnames[hit1].mnem);
		dbox_setfield(dbox_display,display_dbox_field,buf);
	}
	dbox_showstatic(dbox_display);
}   /* end of selected_display_field */





static BOOL dbox_display_raw_handler(dbox d, void *event, void *handle)
/**************************************************************/
{
	wimp_mousestr *mouse;
	int bbits;
	int type;
	wimp_eventstr *e = event;

	/* for which icon numbers do we show the fields menu */
	static char fields_icons[] = {
		0,0,0,0, 1,0,2, 1,0,2, 1,0,2, 1,0,2, 0,0,0,0,0,0
	};

	switch(e->e)
	{
	case wimp_EBUT:
		/* click on icon.  get icon number. */
		mouse = &e->data.but.m;
		bbits = mouse->bbits;

		if((mouse->i) == 19)
		{
			dbox_menu(wmenu_thread_types,selected_thread_type,mouse);
			return(TRUE);
		}

		if((type = fields_icons[mouse->i]) > 0)
		{
			display_dbox_field = mouse->i;

			if(type == 1)
				dbox_menu(wmenu_sort_fields,selected_display_field,mouse);
			else
				dbox_menu(wmenu_display_fields,selected_display_field,mouse);
			return(TRUE);
		}
	}
	return(FALSE);
}   /* end of dbox_display_raw_handler */






static void dbox_display_handler(dbox d, void *handle)
/*********************************************/
{
	int action;
	int  row;
	DISPLAY *dptr;
	DISPLAY_LEVEL *dlevel;
	int error = 0;
	char buf[100];
	char name[32];

	action = dbox_get(d);
	dptr = &display_tab[display_number];

	switch(action)
	{
	case 1:
		dbox_getfield(dbox_display,3,name,31);   /* display name */
		dptr->window_width = dbox_getnumeric(dbox_display,16) * 5;
		dptr->warn = dbox_getnumeric(dbox_display,17);
		dptr->direction = dbox_getnumeric(dbox_display,18);

		for(row=0; row<4; row++)
		{
			dlevel = &dptr->level[row];

			dbox_getfield(dbox_display,row*3+6,buf,sizeof(buf));
			error |= compile_display_line(dptr,row,buf);
			if(error)
				return;

			dlevel->sort_reverse = dbox_getnumeric(dbox_display,row*3+5);
		}
		if(strcmp(name,dptr->name) != 0)
		{
			/* name has been changed */
			strcpy(dptr->name,name);
			make_display_menu();
		}
		display_options_save();

		if(!dbox_persist())
			dbox_hide(d);

		list_open_window_all(2);
		break;

	case 2:   /* remove display entry */
		memset(dptr,0,sizeof(*dptr));
		make_display_menu();
		display_options_save();
		dbox_hide(d);
		break;

	case 20:   /* List sorting data */
		sprintf(buf,"%s.displays",path_choices);
		dataopen(buf,0xfff);
		break;

	default:
		dbox_hide(d);
		break;
	}
}   /* end of dbox_display_handler */







void edit_display_tab(int display_num)
/************************************/
{
	dbox d;
	DISPLAY *dptr;
	DISPLAY_LEVEL *dlevel;
	int  sort;
	char buf[81];

	d = dbox_display;
	display_number = display_num;

	dptr = &display_tab[display_num];

	dbox_setfield(d,3,dptr->name);
	dbox_setnumeric(d,16,dptr->window_width / 5);
	dbox_setnumeric(d,17,dptr->warn);
	dbox_setnumeric(d,18,dptr->direction);
	dbox_setfield(d,19,thread_type_names[dptr->threading]);

	for(sort=0; sort<4; sort++)
	{
		dlevel = &dptr->level[sort];

		dbox_setfield(d,sort*3+4,sortnames[dlevel->sort_field].mnem);
		dbox_setnumeric(d,sort*3+5, dlevel->sort_reverse);

		decompile_display_line(dlevel,buf);
		dbox_setfield(d,sort*3+6,buf);
	}

	dbox_hide(d);
	dbox_showstatic(d);
}   /* end of edit_display_tab */





void display_called(int hit,FOLDREC *fr,int edit)
/***********************************************/
{
	int  i;
	int  count;
	int  card;
	int  card_open;
	DISPLAY *dptr;

	current_list = fr;

	if(hit == 1)
	{
		/* allocate a new display entry */
		for(i=0; i<N_DISPLAY_TAB; i++)
		{
			dptr = &display_tab[i];

			if(dptr->name[0] == 0)
			{
				dptr->window_width = 230 * 4;   /* default */
				dptr->warn = 1;
				edit_display_tab(i);
				return;
			}
		}
		werr(0,"No free display entries");
	}
	else
	{
		/* find display entry for this menu item */
		count=1;
		for(i=0; i<N_DISPLAY_TAB; i++)
		{
			dptr = &display_tab[i];

			if(dptr->name[0] != 0)
			{
				count++;
				if(count == hit)
				{
					break;
				}
			}
		}

		if(i < N_DISPLAY_TAB)
		{
			if(edit==0)
			{
				/* just sort the list */
				visdelay2_begin();
				fr->display = dptr;

				card = -1;
				if((fr->open_levels == fr->display_levels) &&
						(fr->ixlist[fr->cursor_ref] & IXLIST_SELECTED))
				{
					card = (fr->ixlist[fr->cursor_ref] & IXLIST_MASK) << 2;
				}

				card_open = (fr->ixlist[fr->open_article_ref] & IXLIST_MASK) << 2;
				sort_index_list(fr,0);
				list_close_all(fr,1);

				if(card >= 0)
				{
					list_open_at(fr,card,card_open);   /* open at this position */
					list_set_title(fr);
				}
				visdelay2_end();
			}
			else
			{
				edit_display_tab(i);
			}
		}
	}
}   /* end of display_called */







void change_display_mode(OPTIONS_DISPLAY *d, int redraw)
/************************************************/
/* Sets fonts and redraws windows */
{
	int  x;
	int  font_opt;
	int  font_height;
	os_regset regs;
	os_error *err;

	/* find the desktop font name */
	err = os_swi1r(0x400f2+os_X,8,&x);    /* Wimp_ReadSysInfo  desktop font handle */
	desktop_font_name[0] = 0;
	if((err==NULL) && (x != 0))
	{
		os_swi2(0x40083+os_X,x,(int)desktop_font_name);
		x = strlen(desktop_font_name);
		if(desktop_font_name[x-1] <= ' ')
			desktop_font_name[x-1] = 0;   /* remove \r terminator */
	}

	if(options.colours[COLR_LISTS] == 0)
		list_backg_rgb = 0xffffff00;
	else
		list_backg_rgb = 0xdddddd00;

	if(options.colours[COLR_ALIST] == 0)
		alist_backg_rgb = 0xffffff00;
	else
		alist_backg_rgb = 0xdddddd00;

	os_swi3r(0x4008f,0,0,0,&x,&scale_x,&scale_y);      /* Font_ReadScaleFactor */

	for(font_opt=0; font_opt<N_FONTS; font_opt++)
	{
		if(fontn[font_opt] > 0)
			os_swi1(0x40082+os_X,fontn[font_opt]);   /* Font_LoseFont */

		fontn[font_opt] = -1;  /* system font */
		fontname[font_opt] = "System Font";
		err = NULL;

		x = d->font[font_opt].spacing * 32;
		font_spacing[font_opt] = x / (12*16);
		if(font_spacing[font_opt] < 16)
			font_spacing[font_opt] = 16;   /* minimum */

		if(strcmp(d->font[font_opt].name,"System Font") != 0)
		{
			regs.r[0] = -1;
			regs.r[1] = (int)d->font[font_opt].name;
			regs.r[2] = d->font[font_opt].width;
			regs.r[3] = d->font[font_opt].height;
			regs.r[4] = 0;
			regs.r[5] = 0;
			err = os_swix(0x40081,&regs);   /* Font_FindFont */

			if(err == NULL)
			{
				fontn[font_opt] = regs.r[0];
				fontname[font_opt] = d->font[font_opt].name;

				/* find height offset for this font */
				os_swi(0x40084,&regs);      /* Font_ReadInfo */

				font_height = regs.r[4] - regs.r[2];
				if((x = font_height - font_spacing[font_opt]) > 0)
				{
					/* font is too tall to fit in the line spacing */
					font_offset[font_opt] = regs.r[4] - (x+2)/2;
				}
				else
				{
					font_offset[font_opt] = regs.r[4];
				}
			}
		}

		if(fontn[font_opt] <= 0)
		{
			/* system font */
			font_offset[font_opt] = 0;
		}

	}

	standard_font = fontn[FONT_LIST];
	lists_line_height = font_spacing[FONT_LIST];
	lists_font_offset = font_offset[FONT_LIST];

	status_sprite_offset = lists_font_offset - 32;
	if(status_sprite_offset > 0)
		status_sprite_offset = 0;

	if(redraw)
	{
		if(redraw==2)
			list_open_window_all(1);    /* set window extents, don't do this after a mode change */

		redraw_list_lines(NULL,0);   /* redraw all article list windows */
	}
	text_display_change(d,redraw);
}   /* end of change_display_mode */




void change_display_mode2()
/***********************/
{
	change_display_mode(&options.d,1);   /* sets fonts and redraws the windows */

}   /* end of change_display_mode2 */






void init_sprites()
/*****************/
{
	int length;
	int *p;
	os_error *error;
	char fname[256];

	sprintf(fname,"%sSprites15",pluto_path);
	length = get_filelength(fname) + 16;
	status_sprites = malloc(length);
	if(status_sprites == NULL)
		return;

	p = (int *)status_sprites;
	p[0] = length;
	p[2] = 16;
	error = os_swi3(0x2e + os_X,0x100+10,(int)status_sprites,(int)fname);
}   /* end of init_sprites */




void init_display_edit()
/**********************/
{
	menu_sort_fields = menu_new("Fields",menustr_sort_fields);
	wmenu_sort_fields = (wimp_menustr *)menu_syshandle(menu_sort_fields);
	menu_display_fields = menu_new("Fields",menustr_display_fields);
	wmenu_display_fields = (wimp_menustr *)menu_syshandle(menu_display_fields);
	menu_thread_types = menu_new("Thread sort",menustr_thread_types);
	wmenu_thread_types = (wimp_menustr *)menu_syshandle(menu_thread_types);

	dbox_display = dbox_new("Sorts");

	dbox_raw_eventhandler(dbox_display,dbox_display_raw_handler,NULL);
	dbox_eventhandler(dbox_display, dbox_display_handler, NULL);

	display_options_load();

	display_writeout();
}   /* end of init_display_edit */



#define N_CARDS_BINNED   10
extern menu menu_colours;
extern CARD_HEADER card_header;
extern CARD *cards_binned[N_CARDS_BINNED];


extern BOOL button_window_handler(dbox d, void *event, void *handle);
extern void dbox_list_handler(dbox d, void *handle);
// extern void list_menu_proc(void *handle, char *hit);
// extern menu list_menu_maker(void *handle);



void init_cards()
/***************/
{
	menu m;
	menu m_cats;
	menu m_export;
	menu m_score;
	FOLDREC *fr;
	dbox dbox_list;
	int  offset;



	static char *menustr_list = "Selection,Status,Sorting,Search & Select\240F4,Export,Undelete,Select all  ^A";
	static char *menustr_list_selection =
		"Clear   ^Z,Delete  ^X,Remove attachments,Change subject,Change author,Change source,Categories,Colour,Add to address book,Make distribution list,Move to box F6,Copy to box F6,Bounce,Fetch body ^B";
	static char *menustr_list_status = "Unread ^U,New,Read ^E,Lock ^L,Mark ^M,Unlock,Add replied,Remove replied,Set outgoing,Not outgoing,Add tick,Remove tick,Unlink thread,Score";
	static char *menustr_list_cats = "Add at start,Add at end,Delete,Delete all";

	static char *menustr_export = ">Selected articles,>Attachments,>Multi-part,Article list";





	wimp_wind *window_template;

	/* read timezone offset */
	os_swi2r(0x43048,0,0,NULL,&offset);  /* Territory_ReadCurrentTimeZone */
	timezone_offset = offset / 15000;    /* convert to * 10 mins */

	today_date = get_today_date();

	fr = &list_fr[0];
	memset(list_fr,0,sizeof(list_fr));

	/* main list window */
	window_template = template_syshandle("List");

	/* create button bar */
	dbox_list = dbox_new("ListBtns");
	dbox_raw_eventhandler(dbox_list,button_window_handler,(void *)fr);
	dbox_eventhandler(dbox_list, dbox_list_handler, (void *)fr);
	fr->window_buttons = dbox_syshandle(dbox_list);
	fr->dbox_list = dbox_list;


	menu_list = menu_new("Article List",menustr_list);

	menu_selection = menu_new("Selection",menustr_list_selection);
	m_export = menu_new("Export",menustr_export);
	m_cats = menu_new("Categories",menustr_list_cats);

	menu_submenu(menu_selection,7,m_cats);
	menu_submenu(menu_selection,8,menu_colours);
	menu_submenu(menu_list,1,menu_selection);

	m = menu_new("Status",menustr_list_status);
	m_score = menu_new("Score","12345");
	menu_make_writeable(m_score,1,menu_score_value,sizeof(menu_score_value),"");
	menu_submenu(m,14,m_score);

	menu_submenu(menu_list,2,m);

	menu_submenu(menu_list,5,m_export);

//   event_attachmenumaker(w_list,list_menu_maker,list_menu_proc,(void *)&list_fr[0]);

	list_fr[0].type = 0;
	list_fr[0].ixlist = NULL;
	list_fr[0].n_entries = NULL;
	list_fr[0].n_entries_tot = NULL;
	list_fr[0].window = NULL;
//   list_fr[0].window = w_list;
	list_fr[0].display = &display_tab[0];
	strcpy(list_fr[0].name,"All");


	memset(&card_header,0,sizeof(card_header));
	memset(cards_binned,0,sizeof(cards_binned));
	load_box_any_read();

	cardfile_autosave(0,NULL);   /* start periodic timer */
}   /* end of init_cards */

